﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace NASM_Develop_IDE
{
    public partial class NewProjectDialog : Form
    {
        private Solution solution;
        private bool createdSuccesfully;
        public NewProjectDialog()
        {
            InitializeComponent();
            TextBoxHighlighter.Highlight(projectPath, "Enter Project Path");
            TextBoxHighlighter.Highlight(solutionName, "Enter A Valid Solution Name");
            TextBoxHighlighter.Highlight(source, "Enter Source Name (Extension Is Optional)");
            TextBoxHighlighter.Highlight(author, "Enter Name of the Author Of Project (Optionial)");
            ActiveControl = sourcesList;
            solution = null;
            createdSuccesfully = false;
        }

        private void projectPath_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDlg = new FolderBrowserDialog())
            {
                folderDlg.ShowDialog(this);
                folderDlg.RootFolder = Environment.SpecialFolder.MyDocuments;
                projectPath.Text = folderDlg.SelectedPath + "\\";
            }
        }
        public Solution ProjectSolution
        {
            get
            {
                return solution;
            }
        }
        public string Author
        {
            get
            {
                return author.Text;
            }
        }
        public bool SuccessfullyClosedWithOKAY
        {
            get
            {
                return createdSuccesfully;
            }
        }
        private void okay_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(projectPath.Text + solutionName.Text) && sourcesList.Items.Count > 0)
            {
                try
                {
                    DirectoryInfo dirInfo = Directory.CreateDirectory(projectPath.Text + solutionName.Text);
                    DirectoryInfo dirInfo2 = Directory.CreateDirectory(dirInfo.FullName + "\\bin");
                    string pathOfSolution = (dirInfo.Parent.FullName + "\\" +  solutionName.Text + ".nasmproj");
                    Solution s = new Solution();
                    List<string> pathOfSources = new List<string>();
                    foreach (object o in sourcesList.Items)
                    {
                        pathOfSources.Add(dirInfo.FullName + "\\" + (o as string));
                    }
                    s.AbsolutePathOfSources = pathOfSources;
                    s.AbsolutePathOfBinFolder = dirInfo2.FullName;
                    //s.AbsolutePathOfObjectsFiles = new List<string>();
                    solution = s;
                    if (Solution.CreateSolution(pathOfSolution, s))
                    {
                        this.Close();
                        createdSuccesfully = true;
                    }
                    else
                    {
                        MessageBox.Show("Failed To Create Solution! Application Will Close! Please Review The Log", "Application Error",  MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Application.Exit(new CancelEventArgs());
                        createdSuccesfully = false;
                    }
                }
                catch (Exception ex)
                {
                    ExceptionPrinter.Print(ex);
                    MessageBox.Show("Something Went Clearly Wrong During The Project Creation Process. Everything Will Close! Please Review The Handy Log File In The Application's Directory. If It Is Not There--Please Contact Your Local Emergency Center!", "Oh Dear Something Went Wrong", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                    createdSuccesfully = false;
                }
            }
            else
            {
                createdSuccesfully = false;
                ExceptionPrinter.Print(new NothingToWorryAboutException("Project Already Exists In The Selected Project Directory Or No Sources Were Added!"));
                MessageBox.Show("Error Project Already Exists With That Name! Please Retry With A Different Solution Name Or Try A Different Project Location! This Error Could Also Mean That You Have No Sources Added! Please Create One! If The Error Continues To Manifest Itself To The Program. It Might Be Time To Ask Someone Who Knows About These Stick Situations.", "Error In Creating Project!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void NewProjectDialog_Load(object sender, EventArgs e)
        {

        }

        private void addSource_Click(object sender, EventArgs e)
        {
            if (source.Text.EndsWith(".asm"))
            {
                if (!sourcesList.Items.Contains(source.Text))
                {
                    sourcesList.Items.Add(source.Text);
                }
                else
                {
                    MessageBox.Show("Two sources cannot have the same name!", "Source Couldn't Be Added!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                if (!sourcesList.Items.Contains(source.Text + ".asm"))
                {
                    sourcesList.Items.Add(source.Text + ".asm");
                }
                else
                {
                    MessageBox.Show("Two sources cannot have the same name!", "Source Couldn't Be Added!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
    }
}
